package
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	public class Brownian2 extends Sprite
	{
		private var numDots:uint = 20;
		private var friction:Number = 0.95;
		private var dots:Array;
		
		public function Brownian2()
		{
			dots = new Array();
			for (var i:uint = 0; i < numDots; i++) 
			{
				var dot:Ball = new Ball(1, 0);
				dot.x = Math.random() * stage.stageWidth;//随机分布
				dot.y = Math.random() * stage.stageHeight;
				dot.vx = 0;
				dot.vy = 0;
				addChild(dot);
				dots.push(dot);
			}
			graphics.lineStyle(0, 0, 0.2);//绘制路径
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		private function onEnterFrame(event:Event):void 
		{
			for (var i:uint = 0; i < numDots; i++) 
			{
				var dot:Ball = dots[i];
				graphics.moveTo(dot.x, dot.y);
				dot.vx += Math.random() * 0.2 - 0.1;//随机速度向量
				dot.vy += Math.random() * 0.2 - 0.1;
				dot.x += dot.vx;//移动
				dot.y += dot.vy;
				dot.vx *= friction;
				dot.vy *= friction;//摩擦力
				graphics.lineTo(dot.x, dot.y);
			}
		}
	}
}